<template>
  <el-dialog
    v-model="dialogFormVisible"
    :close-on-click-modal="false"
    :title="title"
    width="40%"
    @close="close"
  >
    <el-form ref="formRef" label-width="140px" :model="form" :rules="rules">
      <el-form-item label="书包：" prop="package_id">
        <el-select
          v-model="form.package_id"
          placeholder="请选择书包"
          style="flex-grow: 1"
        >
          <el-option
            v-for="item in bookPackList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="书籍ID：" prop="book_id">
        <el-input
          v-model.trim="form.book_id"
          placeholder="请输入书籍ID"
          type="number"
        />
      </el-form-item>
      <el-form-item label="书籍简介描述：" prop="desc">
        <el-input
          v-model.trim="form.desc"
          placeholder="请输入书籍简介描述"
          rows="4"
          type="textarea"
        />
      </el-form-item>
      <el-form-item label="书籍跳转链接：" prop="url">
        <el-input
          v-model.trim="form.url"
          placeholder="请输入书籍跳转链接"
          rows="4"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { setWeComBook } from '@/api/enterpriseWeChat/weCom'
  export default defineComponent({
    name: 'TableBookEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        formRef: null,
        form: {
          package_id: '',
          book_id: '',
          desc: '',
          url: '',
        },
        rules: {
          package_id: [{ required: true, trigger: 'blur', message: '请选择' }],
          book_id: [
            { required: true, trigger: 'blur', message: '请输入书籍ID' },
          ],
          desc: [
            { required: true, trigger: 'blur', message: '请输入书籍简介描述' },
          ],
          url: [
            { required: true, trigger: 'blur', message: '请输入书籍跳转链接' },
          ],
        },
        title: '',
        bookPackList: [],
        dialogFormVisible: false,
      })
      const showEdit = (bookPackList, row) => {
        state.bookPackList = bookPackList
        if (row) {
          state.title = '编辑推荐书籍'
          state.form = Object.assign({}, row)
          state.dialogFormVisible = true
        } else {
          state.title = '添加推荐书籍'
          state.dialogFormVisible = true
        }
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { message } = await setWeComBook(state.form)
            $baseMessage(message, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }
      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>
<style lang="scss" scoped></style>
